#encoding: utf-8
class ForwardMapsController < ApplicationController
  before_filter :clean_page, :only => [:index]

  def index
    begin
      format = request.headers['HTTP_ACCEPT']
      unless format.include?('application/json')
        @forward = current_user.biz_unit
        init_conditions
      end
      shipowner_id = params[:shipowner_id]
      cy_id = params[:cy_id]
      port_id = params[:port_id]
      voyage = params[:voyage]
      route = params[:route]
      course = params[:course]
      bl_no = params[:bl_no]

      conditions = {}
      if shipowner_id and shipowner_id.strip != ""
        conditions[:shipowner_id] = shipowner_id.strip
      end
      if cy_id and cy_id.strip != ""
        conditions[:cy_id] = cy_id.strip
      end
      if port_id and port_id.strip != ""
        conditions[:port_id] = port_id.strip
      end
      if voyage and voyage.strip != ""
        conditions[:voyage] = voyage.strip
      end
      if route and route.strip != ""
        conditions[:route] = route.strip
      end
      if course and course.strip != ""
        conditions[:course] = course.strip
      end
      if bl_no and bl_no.strip != ""
        conditions[:bl_no] = bl_no.strip
      end

      conditions = init_identity_condition(conditions)

      @bls = Bl.query_bls(conditions, params[:page])

      flash.now[:notice] = "查询完成"
    rescue Exception => ex
      logger.error "#{ex.class}, #{ex.message}"
      flash.now[:notice] = "系统异常，稍候再试"
    end
    respond_to do |format|
      format.html { render :layout => "common" } # index.html.erb
      format.json { render :json => {:notice => flash.now[:notice], :html => render_to_string(:template => '/utils/_bl_and_ctns.html.erb')} }
    end
  end

  private
  def init_conditions
    @routes ||= @forward.routes
    @courses ||= @forward.courses
    @cies ||= @forward.cies
    @ports ||= @forward.ports
    @voyages ||= @forward.voyages
    @shipowners ||= @forward.shipowners

  end


end
